import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class MathProblemsTest {
    public static void runTests() {
        Scanner scanner = new Scanner(System.in);
        int choice = 0;
        boolean validChoice = false;

        while (!validChoice) {
            try {
                System.out.print("请输入一个数字（0 直接结束，1 进行测试）：");
                choice = scanner.nextInt();
                if (choice == 0 || choice == 1) {
                    validChoice = true;
                } else {
                    System.out.println("输入错误，请重新输入。");
                }
            } catch (InputMismatchException e) {
                System.out.println("输入错误，请重新输入。");
                scanner.next();
            }
        }

        if (choice == 0) {
            scanner.close();
            return;
        } else if (choice == 1) {
            int length = 0;
            boolean validLength = false;
            while (!validLength) {
                try {
                    System.out.print("请输入数组的长度：");
                    length = scanner.nextInt();
                    if (length > 0) {
                        validLength = true;
                    } else {
                        System.out.println("长度必须大于 0，请重新输入。");
                    }
                } catch (InputMismatchException e) {
                    System.out.println("输入错误，请输入一个整数。");
                    scanner.next();
                }
            }

            int[] customSizes = new int[length];
            int currentInputCount = 0;
            while (currentInputCount < length) {
                try {
                    System.out.print("请输入数组中的数据：");
                    int inputData = scanner.nextInt();
                    if (currentInputCount < length) {
                        customSizes[currentInputCount] = inputData;
                        currentInputCount++;
                    } else {
                        System.out.println("输入数据过多，请重新输入该数据。");
                        scanner.next();
                    }
                } catch (InputMismatchException e) {
                    System.out.println("输入错误，请输入一个整数。");
                    scanner.next();
                }
                if (currentInputCount < length) {
                    System.out.println("还需输入 " + (length - currentInputCount) + " 个数据。");
                }
            }

            for (int size : customSizes) {
                Random random = new Random();
                HashSet<String> usedExercises = new HashSet<>();
                int count = 0;
                System.out.println("习题组长度为 " + size);
                while (usedExercises.size() < size) {
                    boolean isAddition = random.nextBoolean();
                    if (isAddition) {
                        String additionExercise = MathProblemsGenerator.generateAdditionExercise(random);
                        if (!usedExercises.contains(additionExercise)) {
                            usedExercises.add(additionExercise);
                            System.out.printf("%-15s", additionExercise);
                            count++;
                            if (count % 6 == 0) {
                                System.out.println();
                            }
                        }
                    } else {
                        String subtractionExercise = MathProblemsGenerator.generateSubtractionExercise(random);
                        if (!usedExercises.contains(subtractionExercise)) {
                            usedExercises.add(subtractionExercise);
                            System.out.printf("%-15s", subtractionExercise);
                            count++;
                            if (count % 6 == 0) {
                                System.out.println();
                            }
                        }
                    }
                }
                System.out.println();
            }
        }
        scanner.close();
    }
}